﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StockItem : MonoBehaviour
{
    public Text NameL;
    public Text PriceL;
    public Text RateL;
    public Text HaveNumberL;
    public Text OprationNumberL;
    public float m_PriceNow=0;
    public UtilsGame.CallbackStock ClickedCallback;
    public int OprationNumber { get { return m_OprationNumber; }
        set {
            int hasNumber = Commander.Instance.GetStockNumber(m_Stock.m_ID.ToString());
            
            m_OprationNumber = Mathf.Clamp(value, -hasNumber, 990000000);
        }
    }
    int m_OprationNumber=0;
    public Stock Stock { 
        get { return m_Stock;  } 
        set { 
            m_Stock = value;
            UpdateInformation();
        } 
    }
    Stock m_Stock;


    bool m_LeftFlag     = false;
    bool m_LLeftFlag    = false;
    bool m_RightFlag    = false;
    bool m_RRightFlag   = false;

    float m_TimeCount = 0;
    float m_TimeCountAll = 0;
    void Start()
    {
        
    }

    float RefreshCount =0 ;
    float RefreshCountMax = 1;
    void Update()
    {
        // Refresh
        RefreshCount += Time.deltaTime;
        if (RefreshCount > RefreshCountMax)
        {
            RefreshCount = 0;
            UpdateInformation();
        }

        // Buy
        if (m_LeftFlag || m_LLeftFlag || m_RightFlag || m_RRightFlag)
        {
            m_TimeCount += Time.deltaTime;
            m_TimeCountAll += Time.deltaTime;
        }
        else
        {
            m_TimeCount = 0;
            m_TimeCountAll = 0;
        }

        if(m_TimeCount > 0.7)
        {
            if (m_LeftFlag)
            {
                OprationNumber -= (int)(1.0f * Mathf.Clamp(m_TimeCountAll, 1.0f, 10.0f));
            }
            if (m_LLeftFlag)
            {
                OprationNumber -= (int)(10.0f * Mathf.Clamp(m_TimeCountAll, 1.0f, 10.0f));
            }
            if (m_RightFlag)
            {
                OprationNumber += (int)(1.0f * Mathf.Clamp(m_TimeCountAll, 1.0f, 10.0f));
            }
            if (m_RRightFlag)
            {
                OprationNumber += (int)(10.0f * Mathf.Clamp(m_TimeCountAll, 1.0f, 10.0f));
            }
            m_TimeCount -= Mathf.Max((0.3f - (m_TimeCountAll / 3) * 0.3f), 0.001f);
            UpdateOprationNumber();
        }
    }
    public void UpdateInformation()
    {
        NameL.text = m_Stock.m_Name;
        m_PriceNow = m_Stock.GetPriceNow();
        PriceL.text = "$" + m_PriceNow.ToString("F3");
        float toDayRate = (m_PriceNow - m_Stock.m_ThedayPrice) / m_Stock.m_ThedayPrice * 100f;
        if (toDayRate > 0)
        {
            RateL.color = new Vector4(0, 1, 0, 1);
            RateL.text = "+" + toDayRate.ToString("F3") + "%";
        }
        else
        {
            RateL.color = new Vector4(1, 0, 0, 1);
            RateL.text = toDayRate.ToString("F3") + "%";
        }
        HaveNumberL.text = Commander.Instance.GetStockNumber(m_Stock.m_ID.ToString()).ToString();
        OprationNumberL.text = OprationNumber.ToString();
    }
    void UpdateOprationNumber()
    {
        OprationNumberL.text = OprationNumber.ToString();
    }
    public void ClearOprationNumber()
    {
        OprationNumber = 0;
    }
    public void OnLeftClicked()
    {
        OprationNumber -= 1;
        UpdateOprationNumber();
    }
    public void OnLLeftClicked()
    {
        OprationNumber -= 10;
        UpdateOprationNumber();
    }
    public void OnRightClicked()
    {

        OprationNumber += 1;
        UpdateOprationNumber();
    }
    public void OnRRightClicked()
    {
        OprationNumber += 10;
        UpdateOprationNumber();
    }

    public void OnRightDown()
    {
        m_RightFlag = true;
    }
    public void OnRightUp()
    {
        m_RightFlag = false;
    }
    public void OnRRightDown()
    {
        m_RRightFlag = true;
    }
    public void OnRRightUp()
    {
        m_RRightFlag = false;
    }

    public void OnLeftDown()
    {
        m_LeftFlag = true;
    }
    public void OnLeftUp()
    {
        m_LeftFlag = false;
    }
    public void OnLLeftDown()
    {
        m_LLeftFlag = true;
    }
    public void OnLLeftUp()
    {
        m_LLeftFlag = false;
    }
    public void OnItemClicked()
    {
        if (ClickedCallback != null)
            ClickedCallback(m_Stock);
    }
}
